package com.itheima.controller;


import com.itheima.health.resource.aliyunmessage.SMSUtils;
import com.itheima.health.resource.code.ValidateCodeUtils;
import com.itheima.health.resource.date.DateUtils;
import com.itheima.health.resource.entity.Result;
import com.itheima.health.resource.pub.MessageConstant;
import com.itheima.health.resource.pub.RedisMessageConstant;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("order")
public class OrderControler {

    @Autowired
    private RedisTemplate redisTemplate;

    @Reference
    private OrderService orderService;

    //预约页面验证码
    @RequestMapping("add.do")//用map集合封装要查询的表单数据
    public Result add(@RequestBody Map map){
        try {
            //从前端页面拿用户输入的验证码
            String validateCode =(String) map.get("validateCode");
            //第一步 判断验证码是否有效
            if (StringUtils.isNotBlank(validateCode)){
                //如果验证码不是空的，从前端页面拿到用户输入的手机号，用于和RedisMessageConstant.SENDTYPE_ORDER+telephone的拼接从redis中获取该手机号的验证码
                String telephone = (String) map.get("telephone");
                String key = RedisMessageConstant.SENDTYPE_ORDER+telephone;
                String rediscode =(String) redisTemplate.opsForValue().get(key);
                //判断前端输入的和redis中获取的是否一致
                if (StringUtils.isBlank(rediscode)){
                    return new Result(false,"验证码过期");
                }else if(!rediscode.equals(validateCode)){
                    return new Result(false,"验证码错误");
                }else{
                    //第二部 验证码正确  开始预约，业务层完成
                    Order order = orderService.add(map);
                    String message = (String) map.get("orderDate");
                    System.out.println(message);
                    SMSUtils.sendMessageTuYou(telephone,message);
                    return new Result(true, MessageConstant.ADD_ORDER_SUCCESS,order);
                }
            }else {
                return new Result(false,"数据非法");
            }
        } catch (Exception e) {
            e.printStackTrace();
            //抛出的异常信息
            return new Result(false,e.getMessage());
        }
    }
    @RequestMapping("findOrderMessage.do")//用map集合封装要查询的表单数据
    public Result findOrderMessage(@RequestParam Integer orderid){
        try {
            Map map = orderService.findOrderMessage(orderid);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            //抛出的异常信息
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }


}
